package com.dangf.app.launcher.activity

import android.support.v7.widget.GridLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.dangf.app.launcher.R
import com.dangf.app.launcher.adapter.MainAppListAdapter
import com.dangf.app.launcher.app.BaseActivity
import com.dangf.app.launcher.app.V
import com.dangf.app.launcher.entity.Soft
import kotlinx.android.synthetic.main.activity_admin.*
import pw.hais.utils_lib.utils.KeyboardUtils
import pw.hais.utils_lib.utils.L

/**
 * 系统管理界面
 * Created by hais1992 on 2017/9/16/016.
 */
class AdminActivity : BaseActivity(R.layout.activity_admin), BaseQuickAdapter.OnItemClickListener, View.OnClickListener {
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_close -> finish()
            R.id.btn_ok -> {
                if (V.mStore?.password == edit_admin_passowrd.text.toString()){
                    layout_admin_password.visibility = View.GONE
                    recycler_apps.visibility = View.VISIBLE
                    KeyboardUtils.hideSoftInput(layout_admin_password)
                }else L.showShort("管理密码输入错误！")
            }
        }
    }

    override fun onInitViewsAndData() {
        btn_ok.setOnClickListener(this)
        btn_close.setOnClickListener(this)

        if (V.mAppList != null && V.mAppList!!.isNotEmpty()) {
            val adapter = MainAppListAdapter(V.mSystemAppList!!)
            recycler_apps.adapter = adapter
            recycler_apps.layoutManager = GridLayoutManager(context, 5, GridLayoutManager.VERTICAL, false)
            adapter.onItemClickListener = this@AdminActivity
        }

    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val item = adapter?.getItem(position) as Soft
        startActivity(packageManager.getLaunchIntentForPackage(item.packageName))
        finish()
    }

}